//使用方法<a class="mdchickbox" href="#" data-remote="<{:U('Set/roleList')}>" data-mtitle="编辑操作员" data-id="models" data-width="800" data-height="600">弹窗demo</a>
(function ($) {
	$.fn.mdthickbox = function () {
		//属性使用  data-带上属性值
			var defaults = {
					id: "modal",//弹窗id
					title: "",//弹窗标题
					width: "600",//默认弹窗宽度，暂时不支持%
					height: "500",//默认弹窗高度,不支持%
					backdrop: true,//是否显示遮障
					keyboard: false,//是否开启esc键退出
					remote: ""//iframe调用url
			};

			//动态创建窗口
			var creatDialog = {
					init: function (opts) {
							var _self = this;

							//动态插入窗口
							var d = _self.dHtml(opts);
							$("body").append(d);
							$(".modal-content .modal-header").bind("mousedown", function(event){
								$(".modal-content").append('<div id="modal-white" style="position: absolute; width: 100%; height: calc(100% - 55px); top: 55px; left: 0; z-index: 1;"></div>');
								$moveDiv = $(this).parents(".modal-dialog");
								var offset_x = $moveDiv[0].offsetLeft;
								var offset_y = $moveDiv[0].offsetTop;
								var mouse_x = event.pageX;
								var mouse_y = event.pageY;
								$(document).bind("mousemove", function(ev){
									var _x = ev.pageX - mouse_x;
									var _y = ev.pageY - mouse_y;
									var now_x = (offset_x + _x) + "px";
									var now_y = (offset_y + _y) + "px";
									if ( (offset_x + _x) < 0 ) {
										now_x = "0px";
									}
									if ( (offset_y + _y) < 0 ) {
										now_y = "0px";
									}
									if ( (offset_x + _x + $moveDiv.width()) >= $(window).width() ) {
										now_x = $(window).width() - $moveDiv.width() + "px";
									}
									if ( (offset_y + _y + $moveDiv.height() + 4) >= $(window).height() ) {
										now_y = $(window).height() - $moveDiv.height() - 4 + "px";
									}
									$moveDiv.css({top:now_y,left:now_x});
								});
								$(document).bind("mouseup", function(){
									$("#modal-white").remove();
									$(this).unbind("mousemove");
								});
								return false;
							});
							
							var modal = $("#" + opts.id);

							//初始化窗口
							//modal.modal(opts);								
							modal.modal({
									backdrop: false,
									keyboard: opts.keyboard
							});

							//窗口大小位置
							var h = modal.height() - modal.find(".modal-header").outerHeight() - modal.find(".modal-footer").outerHeight() - 5;

							modal
							//显示窗口
							.modal('show')
							//隐藏窗口后删除窗口html
							.on('hidden', function () {
									modal.remove();
									$(".modal-backdrop").remove();
									if (opts.closeEvent) {
											eval(opts.closeEvent);
									}
							})
							//窗口显示后
							.on('shown', function () {
									if (opts.openEvent) {
											eval(opts.openEvent);
									}
							});
					},
					dHtml: function (o) {
							modal_str = '<div id="' + o.id + '" class="modal fade mdthickbox-modal" aria-hidden="true" tabindex="-1" ><div style=" background-color: #000;bottom: 0;left: 0;position: fixed;right: 0;top: 0;transition: opacity 0.15s linear 0s;opacity: 0.5;"></div><div class="modal-dialog" style="width:' + o.width + 'px;';
							if ( o.title != '' ) {
								modal_str += 'height:' + o.height + 'px;';
							} else {
								modal_str += 'height:' + (o.height -60) + 'px;';
							}
							modal_str += ' margin: 0; top: 30px; left: ' + ($(window).width()-o.width)/2 + 'px;"><div class="modal-content">';
							if ( o.title != '' ) {
								modal_str += '<div class="modal-header" style="cursor: move;"><button type="button" class="close" data-dismiss="modal" ><span aria-hidden="true">×</span></button><h4 class="modal-title" style="cursor: move;">' + o.title + '</h4></div>';
							}
							modal_str += '<iframe width="100%" height="' + (o.height-60) + 'px" frameborder="0" src="' + o.remote + '"></iframe></div></div></div>'; 
							return modal_str;
					}
			};

			return this.each(function () {
					$(this).click(function () {
							var opts = $.extend({}, defaults, {
									id: $(this).attr("data-id"),
									title: $(this).attr("data-mtitle"),
									width: $(this).attr("data-width"),
									height: $(this).attr("data-height"),
									backdrop: $(this).attr("data-backdrop"),
									keyboard: $(this).attr("data-keyboard"),
									remote: $(this).attr("data-remote")
							});
							$(".mdthickbox-modal").remove();
							creatDialog.init(opts);
					});
			});
	};
})(jQuery);
$(".mdthickbox").mdthickbox();

